fix: validate Origin against localhost by default in DNS rebinding protection#840
Open
devcrocod wants to merge 1 commit into
Open
fix: validate Origin against localhost by default in DNS rebinding protection#840devcrocod wants to merge 1 commit into
devcrocod wants to merge 1 commit into
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
Tightens DNS rebinding protection for the Ktor server endpoints by making Origin validation secure-by-default when the server is using the built-in localhost Host allowlist, preventing requests with a valid Host but hostile Origin from being accepted.
Changes:
- Default
allowedOriginsto a localhost origin allowlist whenallowedHostsis not customized (i.e., localhost defaults are in effect). - Add
LOCALHOST_ALLOWED_ORIGINSto support URL-parsable localhost origins. - Add unit tests covering default origin validation behavior for Streamable HTTP.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| kotlin-sdk-server/src/jvmTest/kotlin/io/modelcontextprotocol/kotlin/sdk/server/DnsRebindingProtectionTest.kt | Adds tests asserting default Origin validation behavior when using localhost defaults. |
| kotlin-sdk-server/src/commonMain/kotlin/io/modelcontextprotocol/kotlin/sdk/server/KtorServer.kt | Updates DNS rebinding protection installation to default Origin validation on localhost, and updates related KDoc (partially). |
| kotlin-sdk-server/src/commonMain/kotlin/io/modelcontextprotocol/kotlin/sdk/server/HostValidation.kt | Introduces a localhost Origin allowlist constant used for default Origin validation. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+29
to
+30
| internal val LOCALHOST_ALLOWED_ORIGINS: List<String> = | ||
| listOf("http://localhost", "http://127.0.0.1", "http://[::1]") |
Comment on lines
458
to
466
| private fun Route.installDnsRebindingProtection(enabled: Boolean, hosts: List<String>?, origins: List<String>?) { | ||
| if (!enabled) return | ||
| install(DnsRebindingProtection) { | ||
| allowedHosts = hosts ?: LOCALHOST_ALLOWED_HOSTS | ||
| origins?.let { allowedOrigins = it } | ||
| // Secure-by-default: when relying on the localhost host defaults, validate the Origin | ||
| // header against localhost too, so a request with a valid Host but a hostile Origin | ||
| // (e.g. a DNS-rebinding page) is rejected. Callers with custom hosts opt in explicitly. | ||
| allowedOrigins = origins ?: LOCALHOST_ALLOWED_ORIGINS.takeIf { hosts == null } | ||
| } |
| response.shouldHaveStatus(HttpStatusCode.Forbidden) | ||
| response.bodyAsText() shouldContain "Invalid Origin host: evil.com" | ||
| } | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Default the
Originallowlist to localhost whenDnsRebindingProtectionrelies on the localhost host defaults, so a request with a validHostbut a hostile (ornull)Originis no longer accepted.How Has This Been Tested?
unit tests
Breaking Changes
Behavioral change only
Types of changes
Checklist